package com.hp.ssmp.controller;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.hp.ssmp.entity.TOrder;
import com.hp.ssmp.entity.dto.TOrderDto;
import com.hp.ssmp.service.OrderService;
import com.hp.ssmp.util.R;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.List;

@RestController
@RequestMapping("/order")
public class OrderController {

    @Resource
    private OrderService orderService;

    //    获取订单数量
    @GetMapping("/countNum")
    public R countNum() {
        List<Integer> integers = orderService.countOrderNum();
        return R.success(integers);
    }

    @GetMapping("/countDaysNum")
    public R countDaysNum(Integer day) {
        HashMap<String, List<Object>> stringListHashMap = orderService.countOrderNum(day);
        return R.success(stringListHashMap);
    }

    @GetMapping("/countDaysPrice")
    public R countDaysPrice(Integer day) {
        HashMap<String, List<Object>> stringListHashMap = orderService.countOrderPrice(day);
        return R.success(stringListHashMap);
    }

    @GetMapping("/pageList")
    public R<Page<TOrderDto>> page(Integer currentPage, Integer pageSize, Long orderid, String username, Long userid, Long orderstatus) {
        Page<TOrderDto> orderPage = orderService.getOrderPage(currentPage, pageSize, orderid, username, userid, orderstatus);
        return R.success(orderPage);
    }

    @GetMapping("/getById")
    public R<TOrder> getById(Long orderid) {
        TOrder order = orderService.getById(orderid);
        return R.success(order);
    }

    //    修改订单状态
    @GetMapping("/updStatus")
    public R updStatus(TOrder order) {
        boolean result = orderService.updateById(order);
        if (result) {
            return R.success();
        } else {
            return R.error();
        }
    }

    @PostMapping("/add")
    public R add(TOrder order) {
        boolean result = orderService.addOrder(order);
        if (result) {
            return R.success();
        } else {
            return R.error();
        }
    }

    @GetMapping("/del")
    public R del(Long orderid) {
        boolean result = orderService.removeById(orderid);
        if (result) {
            return R.success();
        } else {
            return R.error();
        }
    }

    @PostMapping("/update")
    public R update(TOrder order) {
        System.out.println("************");
        System.out.println(order);
        boolean result = orderService.updateById(order);
        if (result) {
            return R.success();
        } else {
            return R.error();
        }
    }


}
